/**
* @framework : My Framework
*
* @author: fatangel26@gmail.com
*
*/

jQuery(document).ready(function(){

    var domain = 'http://localhost/my_framework/';
    var addcart = 'index.php?module=cart&action=addcart';
    var viewcart = 'index.php?module=cart&action=viewcart';
    var checkout = 'index.php?module=cart&action=checkout';
    //var url = window.location.host;
    var link_add = domain + addcart;
    var link_view = domain + viewcart;
    var link_checkout = domain + checkout;

    /*******************AJAX CART SCRIPT**********************/
    jQuery('.addcart .cart').live('click', function(eve){
       eve.preventDefault();

       //alert("Clicked!");

       /*Get data from form into vars*/
       var product_id = jQuery(this).parent().find('input[name=product_id]').val();
       var quantity = jQuery(this).parent().find('input[name=quantity]').val();

       //NEED CHECK//
       if(!quantity)
       {
            quantity = 1;
       }

       //var protocol = window.location.protocol;

       var element = '';

       jQuery.ajax({

            url:    link_add,
            type:   "POST",
            data:   "product_id="+product_id+"&quantity="+quantity,
            success:function(result){

                var strJSON = jQuery.parseJSON(result);

                /*element += "<div id=\'lightbox\'></div>";*/

                jQuery("#lightbox").show();

                if(strJSON['error'])
                {
                    element += "<p class=\'lb_msg\'>Chào bạn. Sản phẩm mà bạn lựa chọn đã hết hàng hoặc không còn đủ số lượng. Để biết thêm chi tiết vui lòng liên hệ với chúng tôi qua :</p>";
                    element += "<span class=\'lb_email\'>Email:<a style=\'color: #0DBC6D\' href=\'mailto:fatangel26@gmail.com\'> fatangel26@gmail</a>.</span>";
                    element += "<span class=\'lb_email\'>Điện thoại : 01215151491. </span>";
                }
                else
                {
                    jQuery("#Cart #text_car span").empty().html(strJSON['product_total_quantity']);
                    /*element += "<div id=\'content_lightbox\'>";*/
                    element += "<p class=\'lb_title\'>1 sản phẩm đã được thêm vào giỏ hàng</p>";
                    element += "<div class=\'lb_content\'>";
                    element += "<p class=\'lb_img\'>";
                    element += "<a href=\'" + domain + "san-pham/" + strJSON['menu_slug'] + "/" + strJSON['slug'] + "\'>";
                    element += "<img src=\'" + domain + "admin/" + strJSON['img'] + "\' />";
                    element += "</a>";
                    element += "</p>";/*End .lb_img*/

                    element += "<ul class=\'lb_info\'>";
                    element += "<li class=\'lb_name\'>" + strJSON['product_name'] + "</li>";
                    element += "<li class=\'lb_price\'>" + strJSON['product_price'] + " VNĐ</li>";
                    element += "<li class=\'lb_quantity\'>Số lượng:" + strJSON['product_quantity'] + "</li>";
                    element += "</ul>";/*End .lb_info*/

                    element += "<p class=\'lb_pop\'>";
                    element += "Trị giá đơn hàng: <span class=\'lb_price\'>" + strJSON['product_total_price'] + " VNĐ</span>";
                    element += "<span class=\'giohang\'>Giỏ hàng của bạn có " + strJSON['product_total_quantity'] + " sản phẩm<span>";
                    element += "</p>";/*End .lb_pop*/

                    element += "<div style=\'clear: both\'></div>";

                    element += "<ul class=\'lb_bnt\'>";
                    element += "<li><a href=\'" + domain + "\'>Tiếp tục mua hàng</a></li>";
                    element += "<li><a href=\'" + link_view +  "\'>Xem giỏ hàng</a></li>";
                    element += "<li><a href=\'" + link_checkout + "\'>Đặt hàng và thanh toán</a></li>";
                    element += "</ul>";/*End .lb_bnt*/
                }


                element += "<div class=\'product_relation\' style=\'width:100%\'>";
                element += "<p class=\'a\' style=\'font-size: 2em!important; margin-top: 15px!important;\'>Sản phẩm khác</p>";

                /*NEED CHECK*/
                var check = strJSON['random'].length;

                if(check >0)
                {
                    for(var i=0; i<check; i++)
                    {
                        element += "<div class=\'product_new\'>";
                        element += "<form class=\'addcart\' action=\'\' method=\'POST\'>";
                        element += "<div class=\'text_PRODUCT1\'><p class=\'pro_name\' style=\'height: 20px!important;\'>";
                        element += "<a style=\'color: #80F2F3 !important;\'>" + strJSON['random'][i]['random_product_name'] + "</a></p></div>";
                        element += "<div class=\'img_product\'><p class=\'img_bg\'>";
                        element += "<a href=\'" + domain + "san-pham/" + strJSON['random'][i]['random_menu_slug'] + "/" + strJSON['random'][i]['random_slug'] + "\'><img src=\'" + domain + "admin/" + strJSON['random'][i]['random_product_img'] + "\'/></a></p></div>";
                        element += "<div style=\'clear: both;\'></div>";
                        element += "<div class=\'text_PRODUCT_price\'>" + strJSON['random'][i]['random_product_price'] + " VNÐ</div>";
                        element += "<a class=\'product_icon1 cart\' href=\'#giohang\'><img src=\'" + domain + "assets/frontend/css/img/icon_muahng.png\'></a>";
                        element += "<a class=\'product_icon1\' href=\'" + domain + "san-pham/" + strJSON['random'][i]['random_menu_slug'] + "/" + strJSON['random'][i]['random_slug'] + "\'><img src=\'" + domain + "assets/frontend/css/img/icon_chitiet.png\'></a>";
                        element += "<input type=\'hidden\' value=\'" + strJSON['random'][i]['random_product_id'] + "\' name=\'product_id\' />"
                        element += "</form>";
                        element += "</div>"/*End .product_new*/;
                    }//End for
                }//End if

                element += "</div>";/*End .product_relation*/

                element += "</div>";/*End .lb_content*/
                /*element += "</div>";*/

                jQuery("span.x").find('input[name=quantity]').attr('value',1);

                jQuery("#content_lightbox").html(element).show();


                resize_lightbox();

            }//End success function
       });

       return false;
    });

    /*******************END AJAX CART SCRIPT******************/


    /*******************RESIZE LIGHTBOX**********************/

    var wei = jQuery(window).width();

    var hei = jQuery(window).height();

    function resize_lightbox()
    {
        jQuery('#content_lightbox').css({'left': (wei-650)/2, 'top': (hei-530)/2});
    }

    function resize_img()
    {
        jQuery('#infiLoading').css({'left': (wei-240)/2, 'top': (hei-78)/2});
    }

    /*Hide lightbox*/
    jQuery('#lightbox').live('click',function(eve){

        eve.preventDefault();

        jQuery("#content_lightbox").empty();

        jQuery('#lightbox').fadeOut("fast",function(){
            jQuery('#content_lightbox').hide();
        });

   });

    /*******************************************************/


    /*******************DEL CART*****************************/
    jQuery(".delcart a img").live('click', function(eve){

        eve.preventDefault();

        var product_id = jQuery(this).parent().parent().parent().find('input[name=product_id]').val();
        //console.log(product_id);
        var link = jQuery(this).parent().attr('href');
        //console.log(link);

        jQuery.ajax({

            url:    link,
            type:   "POST",
            data:   "id="+product_id,
            success:function(result){

                   var strJSON = jQuery.parseJSON(result);

                   if(strJSON['view'] == 1)
                   {
                        //window.location.reload();
                        jQuery(".viewcart").empty().append('<p>ABC</p>');
                   }
                   else
                   {
                        jQuery("#Cart #text_car span").empty().html(strJSON['total_items']);
                        jQuery("#cart_ajax").empty().html(strJSON['view']);
                   }

                   //alert(result);
                   /*
                   if(result != 1)
                        jQuery("#cart_ajax").empty().html(result);
                   else
                        window.location.reload();
                   */
            }
        })

        return false;

    });


    /*****************NEED***************/


    if(window.location.href != domain && window.location.href != domain + 'index.php')
    {
        jQuery('html, body').animate({scrollTop: 600},250);
    }

    /*****************CHECK OUT***************/

    jQuery("#checkoutsub").live('click', function(eve){

        eve.preventDefault();
        //alert('Clicked!');
        /*Get data into var*/
        var customer_name       = jQuery(this).parent().parent().find('input[name=checkoutname]').val();
        var customer_phone      = jQuery(this).parent().parent().find('input[name=checkoutphone]').val();
        var customer_email      = jQuery(this).parent().parent().find('input[name=checkoutemail]').val();
        var customer_address    = jQuery(this).parent().parent().find('input[name=checkoutaddress]').val();
        var customer_comment    = jQuery(this).parent().parent().find('textarea[name=comment]').val();
        var captcha             = jQuery(this).parent().parent().find('input[name=captcha]').val();
        var txt_cap             = jQuery(this).parent().parent().find('input[name=txt_cap]').val();

        var link_checkout       = domain + 'index.php?module=cart&action=contact';
        //alert(link_checkout);
        jQuery.ajax({
           url:     link_checkout,
           type:    "POST",
           data:    "customer_name="+customer_name+"&customer_phone="+customer_phone+"&customer_email="+customer_email+"&customer_address="+customer_address+"&customer_comment="+customer_comment+"&captcha="+captcha+"&txt_cap="+txt_cap,
           beforeSend: function(){
                    jQuery("#loading").show();
                    jQuery("#infiLoading").show();
                    resize_img();
           },
           success: function(result){

                jQuery("#loading").hide("fast");
                jQuery("#infiLoading").hide("fast");
                //console.log(result);
                var strJSON = jQuery.parseJSON(result);
                //alert(strJSON);
                //jQuery("#lightbox").hide();
                if(strJSON['error'])
                {
                    jQuery("#contact_name span").empty();
                    jQuery("#contact_name").append(strJSON['error']['customer_name']);
                    jQuery("#contact_phone span").empty();
                    jQuery("#contact_phone").append(strJSON['error']['customer_phone']);
                    jQuery("#contact_email span").empty();
                    jQuery("#contact_email").append(strJSON['error']['customer_email']);
                    jQuery("#contact_address span").empty();
                    jQuery("#contact_address").append(strJSON['error']['customer_address']);
                    jQuery("#captcha span").empty();
                    jQuery("#captcha").append(strJSON['error']['captcha']);
                }

                //alert(strJSON['success'][0]);
                if(strJSON['success'])
                {
                    //alert("Success !");
                    window.location.href = domain + 'index.php?module=cart&action=success';
                }

           }
        });

        return false;
    });

    /****************************************/
})